﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebDomain.Abstract;
using WebDomain.Concrete;
using WebDomain.Entities;
using CZTopComputers.Models;

namespace CZTopComputers.Controllers
{
    public class CartController : Controller
    {
        private IProductRepository repository;
        private EFTicketRepository ticketRepo;
        private EFTicketContainsRepository ticketContainsRepo;

        public CartController()
        {
            repository = new EFProductRepository();
            ticketContainsRepo = new EFTicketContainsRepository();
            ticketRepo = new EFTicketRepository();
        }

        public RedirectToRouteResult AddToCart(int ID, string returnUrl)
        {
            Product product = repository.Products
                .FirstOrDefault(p => p.ID == ID);

            if (product != null)
            {
                GetCart().AddItem(product, 1);
            }
            return RedirectToAction("Index", new { returnUrl });
        }

        public RedirectToRouteResult RemoveOneFromCart(int ID, string returnUrl)
        {
            Product product = repository.Products
                .FirstOrDefault(p => p.ID == ID);

            if (product != null)
            {
                GetCart().AddItem(product, -1);
            }
            return RedirectToAction("Index", new { returnUrl });
        }

        public RedirectToRouteResult RemoveFromCart(int ID, string returnUrl)
        {
            Product product = repository.Products
            .FirstOrDefault(p => p.ID == ID);
            if (product != null)
            {
                GetCart().RemoveLine(product);
            }
            return RedirectToAction("Index", new { returnUrl });
        }

        private Cart GetCart()
        {
            Cart cart = (Cart)Session["Cart"];
            if (cart == null)
            {
                cart = new Cart();
                Session["Cart"]= cart;
            }
            return cart;
        }

        public ActionResult Confirm()
        {
            return View(new CZTopComputers.Models.CustomerModel());
        }

        [HttpPost]
        public ActionResult Confirm(string Name, string phone)
        {
            if (Name == string.Empty || Name == null)
            {
                TempData["message"] = "Name is required";
                return RedirectToAction("Confirm");
            }
            if (GetCart().Lines.Count() > 0)
            {
                Ticket ticket = new Ticket { CustomerName=Name, CustomerPhone=phone };
                ticketRepo.SaveTicket(ticket);
                int ticketID = ticketRepo.GetTicket();
                foreach(var line in GetCart().Lines)
                {
                    for (int i = 0; i < line.Quantity; i++)
                    {
                        TicketContain ticketContain = new TicketContain
                        {
                            TicketID = ticketID,
                            ProductID = line.Product.ID
                        };
                        ticketContainsRepo.SaveTicketContain(ticketContain);
                    }
                }
                GetCart().Clear();
                TempData["message"] = "Cart has been submitted\nYour ticket number is " + ticketID;
            }
            else
            {
                TempData["message"] = "Your cart was empty";
            }
            return RedirectToAction("Index", "Product");
        }

        public ViewResult Index(string returnUrl)
        {
            return View(new CartIndexViewModel
            {
                Cart = GetCart(),
                ReturnUrl = returnUrl
            });
        }
    }
}
